package com.woniuxy.happylife.suggest.service.impl;

import com.woniuxy.happylife.entity.Business;
import com.woniuxy.happylife.suggest.config.MultipartFileToFile;
import com.woniuxy.happylife.suggest.controller.Form;
import com.woniuxy.happylife.suggest.dao.BusinessDao;
import com.woniuxy.happylife.suggest.service.BusinessService;
import com.woniuxy.happylife.suggest.service.IQiniuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @Author Fu
 * @Date 2022-02-21 16:19
 */
@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @Resource
    BusinessDao businessDao;
    @Resource
    IQiniuService qiniuService;
    @Override
    public int insert(MultipartFile[] files, Form form) {
        Business business = new Business();
        business.setCreateTime(form.getCreateTime());
        String image ="";
        String url="";
        for (MultipartFile file : files) {
            try {
                File toFile = MultipartFileToFile.multipartFileToFile(file);
                String key = file.getOriginalFilename() + form.getCreateTime().getTime();
                String result = qiniuService.uploadFile(toFile, key);
                image+=key+",";
                url+=result+",";
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
        String image1 = image.substring(0, image.length() - 1);
        String url1 = url.substring(0, url.length() - 1);

        business.setImage(image1);
        business.setUrl(url1);
        business.setBusinessType(form.getBusinessType());
        business.setContent(form.getContent());
        business.setOrderNo(form.getOrderNo());
        log.info(business.toString());
        int i = businessDao.insert(business);
        return i;
    }

    @Override
    public List<Business> findAll() {
        return businessDao.findAll();
    }

    @Override
    public List<Business> findByCondition(Business business) {
        return businessDao.finByCondition(business);
    }
}
